package com.zzh.partnersys.im.sensitiveword.algorithm;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 敏感词引导类
 *
 * @author zhaoyuhang
 * @date 2023/07/08
 */
@Component
public class SensitiveWordBs {


    /**
     * 脱敏策略,直接获取ACProFilter
     */
    @Autowired
    private SensitiveWordFilter sensitiveWordFilter;


    /**
     * 敏感词列�?
     */
    @Autowired
    private IWordFactory iWordFactory;


//    /**
//     * 初始化静态代码块
//     */
//    {
////        sensitiveWordFilter = new ACProFilter();
////        iWordFactory = new MyWordFactory();
//        List<String> words = iWordFactory.getWordList();
//        loadWord(words);
//    }


    /**
     * 过滤策略
     *
     * @param filter 过滤�?
     * @return 结果
     * @since 0.7.0
     */
    public SensitiveWordBs filterStrategy(SensitiveWordFilter filter) {
        if (filter == null) {
            throw new IllegalArgumentException("filter can not be null");
        }
        this.sensitiveWordFilter = filter;
        return this;
    }

        public SensitiveWordBs sensitiveWord(IWordFactory wordFactory) {
        if (wordFactory == null) {
            throw new IllegalArgumentException("wordFactory can not be null");
        }
        this.iWordFactory = wordFactory;
        return this;
    }


    /**
     * 有敏感词
     *
     * @param text 文本
     * @return boolean
     */
    public boolean hasSensitiveWord(String text) {
        return sensitiveWordFilter.hasSensitiveWord(text);
    }

    /**
     * 过滤
     *
     * @param text 文本
     * @return {@link String}
     */
    public String filter(String text) {
        List<String> wordList = iWordFactory.getWordList();
        loadWord(wordList);
        return sensitiveWordFilter.filter(text);
    }

    /**
     * 加载敏感词列�?
     *
     * @param words 敏感词数�?
     */
    private void loadWord(List<String> words) {
        sensitiveWordFilter.loadWord(words);
    }

}
